package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.core.enums.UserBindEnum;
import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.mapper.UserBindMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.UserBind;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.UserBindVO;
import com.atguigu.srb.core.service.UserBindService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author lwm
 * @since 2022-02-10
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {
    @Resource
    private UserInfoMapper userInfoMapper;
    //生成表单数据
    @Override
    public String genFormData(UserBindVO userBindVO, Long userId) {
        //是否存在身份证号码被他人绑定
        LambdaQueryWrapper<UserBind> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserBind::getIdCard,userBindVO.getIdCard())
                .ne(UserBind::getUserId,userId);
        UserBind userBind = baseMapper.selectOne(wrapper);

        //断言为null，否则抛出身份证已绑定异常
        Assert.isNull(userBind, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);

        //封装UserBind记录
        userBind = new UserBind();
        BeanUtils.copyProperties(userBindVO,userBind);
        userBind.setUserId(userId);
        userBind.setStatus(UserBindEnum.NO_BIND.getStatus());

        //查询用户绑定信息，如果不存在就新建，如果存在就修改
        wrapper.clear();
        wrapper.eq(UserBind::getUserId,userId);
        this.saveOrUpdate(userBind,wrapper);

        //组装表单参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("agentId", HfbConst.AGENT_ID);
        paramMap.put("agentUserId", userId);
        paramMap.put("idCard", userBindVO.getIdCard());
        paramMap.put("personalName", userBindVO.getName());
        paramMap.put("bankType", userBindVO.getBankType());
        paramMap.put("bankNo", userBindVO.getBankNo());
        paramMap.put("mobile", userBindVO.getMobile());
        paramMap.put("returnUrl", HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        paramMap.put("sign", RequestHelper.getSign(paramMap));
        //调用工具类生成拼接好的html页面字符串
        return FormHelper.buildForm(HfbConst.USERBIND_URL, paramMap);
    }

    //更新userBind记录
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBind(Map<String, Object> paramMap) {
        //拿到bindCode和agentUserId
        String bindCode = (String) paramMap.get("bindCode");
        String agentUserId = (String) paramMap.get("agentUserId");
        String resultCode = (String) paramMap.get("resultCode");

        //查询到用户的bind记录
        LambdaQueryWrapper<UserBind> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserBind::getUserId,agentUserId);
        UserBind userBind = baseMapper.selectOne(wrapper);


        //更新用户的基本信息
        UserInfo userInfo = new UserInfo();
        userInfo.setId(Long.valueOf(agentUserId));
        userInfo.setName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());

        //设置绑定状态
        if (resultCode.equals("0001")){
            userBind.setBindCode(bindCode);
            userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
            userInfo.setBindCode(bindCode);
            userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
        }else {
            userBind.setStatus(UserBindEnum.BIND_FAIL.getStatus());
            userInfo.setBindStatus(UserBindEnum.BIND_FAIL.getStatus());
        }

        //更新用户绑定表   bindCode status
        baseMapper.updateById(userBind);
        //更新用户信息表  bindCode Name IdCard BindStatus
        userInfoMapper.updateById(userInfo);

    }

    //根据userid获取投资人的绑定协议号
    @Override
    public String getBindCodeByUserId(Long userId) {
        LambdaQueryWrapper<UserBind> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserBind::getUserId,userId);
        UserBind userBind = baseMapper.selectOne(wrapper);
        return userBind.getBindCode();
    }
}
